package com.zzgele.qmzb.api.utils;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.zzgele.qmzb.api.core.Constants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


public class JGPush {
	
	    private static JPushClient jpushClient = null;
	    private static PushPayload payload = null;
	    private static final String appKey = Constants.JGKEY;
		private static final String appSecret = Constants.JGSECRT;
	private final Logger logger = LoggerFactory.getLogger(JGPush.class);


	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String tag, String content,String icon,String nickname,Long number,String cid,String pull_url,Long uid, String type) {
			return PushPayload.newBuilder()
					.setPlatform(Platform.all())
					.setOptions(Options.newBuilder().setApnsProduction(false).setTimeToLive(0).build())
					.setAudience(Audience.tag(tag))
					.setNotification(Notification.newBuilder()
							.addPlatformNotification(IosNotification.newBuilder()
									.setAlert(content)
									.setSound("default")
									.addExtra("type", type)
									.addExtra("roomid", number)
									.addExtra("icon", icon)
									.addExtra("nickname", nickname)
									.addExtra("content", content)
									.addExtra("uid", uid)
									.addExtra("cid", cid)
									.addExtra("pull_url", pull_url)
									.build())
							.addPlatformNotification(AndroidNotification.newBuilder()
									.setAlert(content)
									.addExtra("type", type)
									.addExtra("roomid", number)
									.addExtra("icon", icon)
									.addExtra("nickname", nickname)
									.addExtra("content", content)
									.addExtra("uid", uid)
									.addExtra("cid", cid)
									.addExtra("pull_url", pull_url)
									.build())
							.build())
					.build();
	    }

		
		public static void push(String tag, String content,String icon,String nickname,Long number,String cid,String pull_url,Long uid, String type){
			System.out.println(appKey.length()+"-"+appSecret.length());
			jpushClient = new JPushClient(appSecret, appKey);
			payload = buildPushObject_ios_audienceMore_messageWithExtras(tag,content,icon,nickname,number,cid,pull_url,uid,type);
			try {
	            PushResult result = jpushClient.sendPush(payload);
	            System.out.println(result.toString());
	        } catch (APIConnectionException  e) {
	        } catch (APIRequestException  e) {
	        }
		}

	public static PushPayload buildPushObject_all_alias_alert(List<String> alias, String content, String type) {
		return PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setOptions(Options.newBuilder().setApnsProduction(false).setTimeToLive(0).build())
				.setAudience(Audience.alias(alias))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder()
								.setAlert(content)
								.setSound("default")
								.addExtra("type", type)
								.addExtra("content", content)
								.build())
						.addPlatformNotification(AndroidNotification.newBuilder()
								.setAlert(content)
								.addExtra("type", type)
								.addExtra("content", content)
								.build())
						.build())
				.build();
	}


	public static void push1(List<String> alias,String content,String type){
		jpushClient = new JPushClient(appSecret, appKey);
		payload = buildPushObject_all_alias_alert(alias,content,type);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result.toString());
		} catch (APIConnectionException e) {
		} catch (APIRequestException e) {
		}
	}



}
